<script>
import CourseApi from "../api/course";

export default {
  data() {
    return {
      dataList: [], //数据集合
      addDialogVisible: false, //新增数据对话框控制
      addCourse: {
        name: "",
        credit: "",
        hours: "",
        teacher: ""
      }, // 新增数据的格式
      editDialogVisible: false, //编辑数据对话框控制
      editCourse: {
        id: 0,
        name: "",
        credit: "",
        hours: "",
        teacher: ""
      }, // 编辑数据的格式
      deleteCourse: {
        id: 0,
        name: "",
        credit: "",
        hours: "",
        teacher: ""
      }, // 删除数据的格式
      currentPage: 1,
      pageSize: 10,
      total: 0
    };
  },
  methods: {
    async getList() {
      //TODO:网络访问错误的处理
      let api = new CourseApi();
      // this.dataList = await api.getList();
      this.dataList = await api.getList(this.currentPage, this.pageSize);
      console.log(this.dataList);
    },
    async getCount() {
      let api = new CourseApi();
      this.total = await api.getCount();
      console.log(this.total);
    },
    async getData() {
      await this.getCount();
      await this.getList();
    },
    handleCurrentChange() {
      console.log(this.currentPage);
      this.getList();
    },
    indexMethod(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    showAddDialog() {
      this.addDialogVisible = true;
    },
    async addSubmit() {
      if (!this.$refs.formAdd) return;
      let formEl = this.$refs.formAdd;
      await formEl.validate(async (valid) => {
        if (valid) {
          let api = new CourseApi();
          await api.add(this.addCourse);
          formEl.resetFields(); //清空表单
          this.addDialogVisible = false; //关闭对话框
          this.getData(); //刷新数据列表
        } else {
          console.log("error submit!");
          return false;
        }
      });
    },
    addReset() {
      if (!this.$refs.formAdd) return;
      let formEl = this.$refs.formAdd;
      formEl.resetFields(); //重置表单
      this.addDialogVisible = false;
    },
    showEditDialog(index, row) {
      console.log(index, row);
      this.editDialogVisible = true;
      this.editCourse = row; //需要编辑的数据行
    },
    async editSubmit() {
      if (!this.$refs.formEdit) return;
      let formEl = this.$refs.formEdit;
      await formEl.validate(async (valid) => {
        if (valid) {
          let api = new CourseApi();
          await api.edit(this.editCourse);
          formEl.resetFields(); //清空表单
          this.editDialogVisible = false; //关闭对话框
          this.getData(); //刷新数据列表
        } else {
          console.log("error submit!");
          return false;
        }
      });
    },
    editReset() {
      if (!this.$refs.formEdit) return;
      let formEl = this.$refs.formEdit;
      formEl.resetFields(); //重置表单
      this.editDialogVisible = false;
    },
    showDeleteDialog(index, row) {
      console.log(index, row);
      // eslint-disable-next-line no-undef
      ElMessageBox.confirm("确认删除吗？", "删除记录", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          this.deleteCourse = row;
          let api = new CourseApi();
          await api.delete(this.deleteCourse.id);
          this.getData();
          // eslint-disable-next-line no-undef
          ElMessage({
            type: "success",
            message: "记录已删除"
          });
        })
        .catch(() => {
          // eslint-disable-next-line no-undef
          ElMessage({
            type: "info",
            message: "删除已终止"
          });
        });
    }
  },
  mounted() {
    // this.getList();
    this.getData();
  }
};
</script>

<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/manager/course' }">课程管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider />

    <!-- 功能区 -->

    <el-row class="row-bg" justify="space-between">
      <el-col :span="4">
        <h3>课程管理</h3>
      </el-col>

      <el-col :span="4">
        <el-button type="primary" @click="showAddDialog">新增</el-button>
      </el-col>
    </el-row>

    <!-- 数据列表 -->
    <el-table :data="dataList" style="width: 100%" height="60vh" empty-text="暂无数据" border>
      <el-table-column type="index" label="序号" width="100" align="center" :index="indexMethod" />
      <el-table-column prop="name" label="课程名" align="center" />
      <el-table-column prop="credit" label="学分" align="center" />
      <el-table-column prop="hours" label="学时" align="center" />
      <el-table-column prop="teacher" label="授课教师" align="center" />
      <el-table-column fixed="right" width="200">
        <template #default="scope">
          <el-button type="warning" size="small" @click="showEditDialog(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button type="danger" size="small" @click="showDeleteDialog(scope.$index, scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->

    <el-pagination
      v-if="total > 0"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      background
      layout="prev, pager, next" />

    <!-- 新增数据 -->

    <el-dialog v-model="addDialogVisible" title="新增">
      <el-form :model="addCourse" label-width="120px" ref="formAdd">
        <el-form-item
          label="课程名"
          prop="name"
          :rules="[{ required: true, message: '课程名为必填项' }]">
          <el-input v-model="addCourse.name" placeholder="输入课程名" />
        </el-form-item>
        <el-form-item label="学分" prop="credit">
          <el-input v-model="addCourse.credit" placeholder="输入学分" />
        </el-form-item>
        <el-form-item label="学时" prop="hours">
          <el-input v-model="addCourse.hours" placeholder="输入学时" />
        </el-form-item>
        <el-form-item label="授课教师" prop="teacher">
          <el-input v-model="addCourse.teacher" placeholder="输入授课教师" />
        </el-form-item>
        <el-form-item>
          <el-button @click="addReset">取消</el-button>
          <el-button type="primary" @click="addSubmit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 编辑数据 -->

    <el-dialog v-model="editDialogVisible" title="编辑">
      <el-form :model="editCourse" label-width="120px" ref="formEdit">
        <el-form-item
          label="课程名"
          prop="name"
          :rules="[{ required: true, message: '课程名为必填项' }]">
          <el-input v-model="editCourse.name" placeholder="输入课程名" />
        </el-form-item>
        <el-form-item label="学分" prop="credit">
          <el-input v-model="editCourse.credit" placeholder="输入学分" />
        </el-form-item>
        <el-form-item label="学时" prop="hours">
          <el-input v-model="editCourse.hours" placeholder="输入学时" />
        </el-form-item>
        <el-form-item label="授课教师" prop="teacher">
          <el-input v-model="editCourse.teacher" placeholder="输入授课教师" />
        </el-form-item>
        <el-form-item>
          <el-button @click="editReset">取消</el-button>
          <el-button type="primary" @click="editSubmit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped>
.el-row {
  margin-bottom: 16px;
}

.el-table {
  margin-bottom: 16px;
}
</style>
